﻿using famails.common;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace famails.App_Code.source.dao
{
    public class CM_CustomerCareAuto_ContentDAO
    {
        internal DataTable GetByCustomerCareAutoId(int ID, int UserID)
        {
            SqlCommand cmd = new SqlCommand("SP_CM_CustomerCareAuto_Content_Get4Edit", ConnectionData._MyConnection);
            cmd.CommandType = CommandType.Text;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@CustomerCareAutoID", SqlDbType.Decimal).Value = ID;
            cmd.Parameters.Add("@UserID", SqlDbType.Int).Value = UserID;
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataTable table = new DataTable();
            adapter.Fill(table);
            cmd.Dispose();
            adapter.Dispose();
            return table;
        }

        internal int Add(global::famails.dto.CM_CustomerCareAuto_ContentDTO dt)
        {
            string sql = "INSERT INTO tblCM_CustomerCareAuto_Content(CustomerCareAutoId, ContentId, HourSend, OrderNo, subject, body, createdate) " +
                         "VALUES(@CustomerCareAutoId, @ContentId, @HourSend, @OrderNo, @subject, @body, getdate()) select @@identity";
            SqlCommand cmd = new SqlCommand(sql, ConnectionData._MyConnection);
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.Add("@CustomerCareAutoId", SqlDbType.Int).Value = dt.CustomerCareAutoId;
            cmd.Parameters.Add("@ContentId", SqlDbType.NVarChar).Value = dt.ContentId;
            cmd.Parameters.Add("@HourSend", SqlDbType.NVarChar).Value = dt.HourSend;
            cmd.Parameters.Add("@OrderNo", SqlDbType.NVarChar).Value = dt.OrderNo;
            cmd.Parameters.Add("@subject", SqlDbType.NVarChar).Value = dt.Subject;
            cmd.Parameters.Add("@body", SqlDbType.NVarChar).Value = dt.Body;
            object obj = cmd.ExecuteScalar();
            cmd.Dispose();

            int result = 0;
            int.TryParse(obj+"", out result);
            return result;
        }

        internal int Edit(global::famails.dto.CM_CustomerCareAuto_ContentDTO dt)
        {
            string sql = "UPDATE tblCM_CustomerCareAuto_Content SET " +
                        "CustomerCareAutoId = @CustomerCareAutoId, " +
                        "ContentId = @ContentId, " +
                        "HourSend = @HourSend, " +
                        "OrderNo = @OrderNo, " +
                        "subject = @subject, " +
                        "body = @body, LastModifyDate=getdate() " +
                        " WHERE Id = @Id";
            SqlCommand cmd = new SqlCommand(sql, ConnectionData._MyConnection);
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.Add("@Id", SqlDbType.Int).Value = dt.Id;
            cmd.Parameters.Add("@CustomerCareAutoId", SqlDbType.Int).Value = dt.CustomerCareAutoId;
            cmd.Parameters.Add("@ContentId", SqlDbType.NVarChar).Value = dt.ContentId;
            cmd.Parameters.Add("@HourSend", SqlDbType.NVarChar).Value = dt.HourSend;
            cmd.Parameters.Add("@OrderNo", SqlDbType.NVarChar).Value = dt.OrderNo;
            cmd.Parameters.Add("@subject", SqlDbType.NVarChar).Value = dt.Subject;
            cmd.Parameters.Add("@body", SqlDbType.NVarChar).Value = dt.Body;
            int rs = cmd.ExecuteNonQuery();
            cmd.Dispose();
            return rs;
        }
    }
}